<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Report_penerimaan_kasir extends Authenticated_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('pencabang_model', 'cabang');
		$this->load->model('setoran_kasir_model', 'kasir');
	}

	function index() {
		$this->list_result();
	}

	function list_result($tgl_awal='', $tgl_akhir='') {
		$data['filter_cabang'] = $this->cabang->get_all_cabang();

		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'm/d/Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'm/d/Y');
		}

		$data['list_result'] = $this->kasir->get_kasir($tgl_awal, $tgl_akhir);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['cabang'][$i] = $this->kasir->get_cabang_by_user($data['list_result'][$i]['idkasirSetoran']);
			$data['detail'] = $this->kasir->get_penerimaan_kasir($data['list_result'][$i]['idkasirSetoran']);
		}

		$this->load->view('template/header_view');
		$this->load->view('laporan/laporan_penerimaan_kasir', $data);
		$this->load->view('template/footer_view');
	}
	
	function cetak_pdf($tgl_awal='', $tgl_akhir=''){
		$data['filter_cabang'] = $this->cabang->get_all_cabang();

		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'd-m-Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'd-m-Y');
		}

		$data['list_result'] = $this->kasir->get_kasir($tgl_awal, $tgl_akhir);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['cabang'] = $this->kasir->get_cabang_by_user($data['list_result'][$i]['idkasirSetoran']);
			$data['detail'] = $this->kasir->get_penerimaan_kasir($data['list_result'][$i]['idkasirSetoran']);
		}
		$html=$this->load->view('laporan/html_cetak_penerimaan_kasir', $data,TRUE);
		$this->load->library('pdf');
		$this->pdf->to_pdf_landscape($html,'Report Penerimaan Kasir.pdf');
	}

	function cetak_excel($tgl_awal='', $tgl_akhir=''){
		$this->load->library('excel');
		$data['filter_cabang'] = $this->cabang->get_all_cabang();

		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'd-m-Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'd-m-Y');
		}

		$data['list_result'] = $this->kasir->get_kasir($tgl_awal, $tgl_akhir);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['cabang'] = $this->kasir->get_cabang_by_user($data['list_result'][$i]['idkasirSetoran']);
			$data['detail'] = $this->kasir->get_penerimaan_kasir($data['list_result'][$i]['idkasirSetoran']);
		}
		
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle('title')->setDescription('description');

		$objPHPExcel->setActiveSheetIndex(0);
		//Set Keterangan
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Penerimaan Kasir');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Periode');
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $data['tgl_awal'].'-'.$data['tgl_akhir']);
		//Set header
		$objPHPExcel->getActiveSheet()->setCellValue('A4', 'No');
		$objPHPExcel->getActiveSheet()->setCellValue('B4', 'Nama Kasir');
		$objPHPExcel->getActiveSheet()->setCellValue('C4', 'Cabang');
		$objPHPExcel->getActiveSheet()->setCellValue('D4', 'Telp');
		$objPHPExcel->getActiveSheet()->setCellValue('E4', 'Total Tiket');
		$objPHPExcel->getActiveSheet()->setCellValue('F4', 'Total Tiket Void');
		$objPHPExcel->getActiveSheet()->setCellValue('G4', 'Total Omset');
		$objPHPExcel->getActiveSheet()->setCellValue('H4', 'Total Discount');
		$objPHPExcel->getActiveSheet()->setCellValue('I4', 'Total Fee Agen');
		$objPHPExcel->getActiveSheet()->setCellValue('J4', 'Total Void');
		$objPHPExcel->getActiveSheet()->setCellValue('K4', 'Total Biaya Langsung');
		$objPHPExcel->getActiveSheet()->setCellValue('L4', 'Total Setor');
		// Save it as an excel 2003 file
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		
		$row=5;
		$sum = array(
			'total_omset'=>0,
			'total_discount'=>0,
			'fee_agen'=>0,
			'total_void'=>0,
			'total_biaya_langsung'=>0,
			'total_setoran'=>0
		);
		for($i=0;$i<count($data['list_result']);$i++){
			
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, ($i+1));
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $data['list_result'][$i]['nama']);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $data['cabang']['Nama']);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $data['cabang']['Telp']);
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $data['detail'][$i]['total_penumpang']);
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $data['detail'][$i]['tiket_void']);
			
			$sum['total_omset'] += $data['detail'][$i]['total_omset'];
			$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, 'Rp. '.number_format($data['detail'][$i]['total_omset'],0,',','.'));
			
			$sum['total_discount'] += $data['detail'][$i]['total_discount'];
			$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, 'Rp. '.number_format($data['detail'][$i]['total_discount'],0,',','.'));
			
			$sum['fee_agen'] += $data['detail'][$i]['fee_agen'];
			$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, 'Rp. '.number_format($data['detail'][$i]['fee_agen'],0,',','.'));
			
			$sum['total_void'] += $data['detail'][$i]['total_void'];
			$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, 'Rp. '.number_format($data['detail'][$i]['total_void'],0,',','.'));
			
			$sum['total_biaya_langsung'] += $data['detail'][$i]['jumlah'];
			$objPHPExcel->getActiveSheet()->setCellValue('K'.$row, 'Rp. '.number_format($data['detail'][$i]['jumlah'],0,',','.'));
			$sum['total_setoran'] += $data['detail'][$i]['total_setoran'];
			$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, 'Rp. '.number_format($data['detail'][$i]['total_setoran'],0,',','.'));
		
			$row++;
		}
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, 'Total');
		$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $sum['total_omset']);
		$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $sum['total_discount']);
		$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $sum['fee_agen']);
		$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, $sum['total_void']);
		$objPHPExcel->getActiveSheet()->setCellValue('K'.$row, $sum['total_biaya_langsung']);
		$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, $sum['total_setoran']);
		
		$objWriter->save('public/Report Penerimaan Kasir '.$tgl_awal.'-'.$tgl_akhir.'.xls');
		redirect(PUBLIC_FOLDER.'Report Penerimaan Kasir '.$tgl_awal.'-'.$tgl_akhir.'.xls');
	}
	
}